<?php
    /*

     Version: MPL 1.1

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is KASSIOP Framework.

     The Initial Developer of the Original Code is SURIAN Nicolas (aka NairuS) <me@nairus.fr>.
     Portions created by the Initial Developer are Copyright (C) 2010
     The Initial Developer. All Rights Reserved.

     Contributor(s):

     Alternatively, the contents of this file may be used under the terms
     of the MPL license Version 1.1 (the  "MPL" License"), in which case the
     provisions of Version: MPL 1.1 License are applicable instead of those
     above.  If you wish to allow use of your version of this file only
     under the terms of the MPL License and not to allow others to use
     your version of this file under the MPL, indicate your decision by
     deleting  the provisions above and replace  them with the notice and
     other provisions required by the MPL License.  If you do not delete
     the provisions above, a recipient may use your version of this file
     under either the MPL License.

     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

    */


    require_once 'PHPUnit/Framework.php';
    require_once dirname( __FILE__ ) . '/../../../kassiop/core/Autoload.php' ;
    Autoload::register() ;

    /**
     * The unit test class of the CredentialManager class.
     *
     * @package    unittests.kassiop
     * @subpackage managers
     *
     * @author NairuS
     * @version 1.3 $Rev: 90 $ $Date: 2011-01-16 09:23:26 +0000 (Sun, 16 Jan 2011) $ $Author: nicolas.surian $
     *
     */
    class ConfigManagerTest extends PHPUnit_Framework_TestCase
    {
        /**
         * Defines the CredentialsManagerClass to test.
         *
         * @var ConfigManager
         */
        protected $manager ;

        protected function setUp()
        {
            $this->manager = new ConfigManager() ;
        }

        protected function tearDown()
        {
            unset( $this->manager );
        }

        protected function changeManager()
        {
            Configuration::includeBootstrap() ;
            $this->manager = new ConfigManager( CONFIG_MAIN_PATH . ConfigManager::DEFAULT_CONFIG_FILE_NAME ) ;
        }

        public function testConstructor()
        {
            try
            {
                $this->changeManager();

            }
            catch( Exception $e )
            {
                $this->assertEquals( $e->getMessage(), 'The config file "' . CONFIG_MAIN_PATH . ConfigManager::DEFAULT_CONFIG_FILE_NAME .'" cannot be parsed!', "The exception message must be well setted" );
            }
            $this->assertEquals( $this->manager->get( DATABASE_HOST_PROPERTY, DATABASE_SECTION ), 'localhost', '1.1 The host value of the database section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( DATABASE_NAME_PROPERTY, DATABASE_SECTION ), 'kassiopdb', '1.2 The name value of the database section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( DATABASE_PASS_PROPERTY, DATABASE_SECTION ), ''         , '1.3 The pass value of the database section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( DATABASE_USER_PROPERTY, DATABASE_SECTION ), 'root'     , '1.4 The user value of the database section must be correct.' ) ;

            $this->assertEquals( $this->manager->get( APPLICATIONS_DEFAULT_LANG_PROPERTY     , APPLICATIONS_SECTION ), 'fr', '2.1 The defaultLang value of the applications section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( APPLICATIONS_DEFAULT_TIME_ZONE_PROPERTY, APPLICATIONS_SECTION ), 'Europe/Paris', '2.2 The defaultTimeZone value of the applications section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( APPLICATIONS_DOCUMENT_ROOT_PROPERTY    , APPLICATIONS_SECTION ), '', '2.3 The documentRoot value of the applications section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( APPLICATIONS_LIBS_PROPERTY             , APPLICATIONS_SECTION ), 'applications', '2.4 The libs value of the applications section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( APPLICATIONS_PROJECT_NAME_PROPERTY     , APPLICATIONS_SECTION ), 'kassiop', '2.5 The projectName value of the applications section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( APPLICATIONS_SERVER_NAME_PROPERTY      , APPLICATIONS_SECTION ), 'local.kassiop', '2.6 The serverName value of the applications section must be correct.' ) ;

            $this->assertEquals( $this->manager->get( NAME_PROPERTY    , FRONTEND_SECTION ), 'frontend', '3.1 The name value of the frontend section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( ROOT_DIR_PROPERTY, FRONTEND_SECTION ), '', '3.2 The rootDir value of the frontend section must be correct.' ) ;

            $this->assertEquals( $this->manager->get( NAME_PROPERTY    , BACKEND_SECTION ), 'backend', '4.1 The name value of the backend section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( ROOT_DIR_PROPERTY, BACKEND_SECTION ), 'administration', '4.2 The rootDir value of the backend section must be correct.' ) ;

            $this->assertTrue( is_array( $this->manager->get('stable', 'versions') ) , '5.1 The stable property of versions section must be an array' );
            $this->assertTrue( is_array( $this->manager->get('beta', 'versions') ) , '5.2 The stable property of versions section must be an array' );
        }

        public function testGet()
        {
            Configuration::includeBootstrap() ;

            $this->assertEquals( $this->manager->get( DATABASE_HOST_PROPERTY, DATABASE_SECTION ), 'localhost', '1.1 The host value of the database section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( DATABASE_NAME_PROPERTY, DATABASE_SECTION ), 'kassiopdb', '1.2 The name value of the database section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( DATABASE_PASS_PROPERTY, DATABASE_SECTION ), 'root'     , '1.3 The pass value of the database section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( DATABASE_USER_PROPERTY, DATABASE_SECTION ), 'root'     , '1.4 The user value of the database section must be correct.' ) ;

            $this->assertEquals( $this->manager->get( APPLICATIONS_DEFAULT_LANG_PROPERTY     , APPLICATIONS_SECTION ), 'fr', '2.1 The defaultLang value of the applications section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( APPLICATIONS_DEFAULT_TIME_ZONE_PROPERTY, APPLICATIONS_SECTION ), 'Europe/Paris', '2.2 The defaultTimeZone value of the applications section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( APPLICATIONS_DOCUMENT_ROOT_PROPERTY    , APPLICATIONS_SECTION ), '', '2.3 The documentRoot value of the applications section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( APPLICATIONS_LIBS_PROPERTY             , APPLICATIONS_SECTION ), 'applications', '2.4 The libs value of the applications section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( APPLICATIONS_PROJECT_NAME_PROPERTY     , APPLICATIONS_SECTION ), 'kassiop', '2.5 The projectName value of the applications section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( APPLICATIONS_SERVER_NAME_PROPERTY      , APPLICATIONS_SECTION ), 'local.kassiop', '2.6 The serverName value of the applications section must be correct.' ) ;

            $this->assertEquals( $this->manager->get( NAME_PROPERTY    , FRONTEND_SECTION ), 'frontend', '3.1 The name value of the frontend section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( ROOT_DIR_PROPERTY, FRONTEND_SECTION ), '', '3.2 The rootDir value of the frontend section must be correct.' ) ;

            $this->assertEquals( $this->manager->get( NAME_PROPERTY    , BACKEND_SECTION ), 'backend', '4.1 The name value of the backend section must be correct.' ) ;
            $this->assertEquals( $this->manager->get( ROOT_DIR_PROPERTY, BACKEND_SECTION ), 'administration', '4.2 The rootDir value of the backend section must be correct.' ) ;
        }

        public function testGetBackConfigPath()
        {
            $exceptedDir = realpath( dirname( __FILE__ ) . '/../../..' ) . '/applications/backend/config/' ;
            $this->assertEquals( $this->manager->getBackConfigPath(), $exceptedDir, 'The front config path must be well setted.' );

        }

        public function testGetFrontConfigPath()
        {
            $exceptedDir = realpath( dirname( __FILE__ ) . '/../../..' ) . '/applications/frontend/config/' ;
            $this->assertEquals( $this->manager->getFrontConfigPath(), $exceptedDir, 'The front config path must be well setted.' );
        }
    }
?>